<template>
  <div class="user-center">
    <div class="back" @click="back">
      <i class="icon-back"></i>
    </div>
    <div class="switches-wrapper">
      <switches :switches="switches" :switchIndex="switchIndex" @switchOne="switchOne"></switches>
    </div>
    <div ref="playBtn" class="play-btn">
      <i class="icon-play"></i>
      <span class="text" @click.stop="randomPlayAll">随机播放全部</span>
    </div>
    <div class="list-wrapper" ref="listWrapper">
      <scroll ref="refFavorite" v-if="switchIndex===0" class="list-scroll" :data="favoriteList">
        <div class="list-inner">
          <song-list :data="favoriteList" @selectItem="selectSong"></song-list>
        </div>
      </scroll>
      <scroll ref="refHistory" v-if="switchIndex===1" class="list-scroll" :data="playHistory">
        <div class="list-inner">
          <song-list :data="playHistory" @selectItem="selectSong"></song-list>
        </div>
      </scroll>
    </div>
    <div class="no-result-wrapper">
      <no-result :title="noResultTitle" v-show="noResult"></no-result>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
  import { mapGetters, mapActions } from 'vuex'
  import Switches from 'base/switches/switches'
  import Scroll from 'base/scroll/scroll'
  import Song from 'common/js/song'
  import SongList from 'base/song-list/song-list'
  import noResult from 'base/no-result/no-result'
  import { playlistMixin } from 'common/js/mixin'

  export default {
    name: 'user-center',
    mixins: [ playlistMixin ],
    components: {
      Switches,
          Scroll,
          SongList,
          noResult
    },
    data() {
      return {
        switchIndex: 0,
        switches: [
          {
            name: '我喜欢的'
          },
          {
            name: '最近听的'
          }
        ]
      }
    },
    methods: {
      randomPlayAll() {
        let list = null
        if (this.switchIndex === 0) {
          list = this.favoriteList
        } else {
          list = this.playHistory
        }
        list = list.map((item) => {
          return new Song(item)
        })
        this.randomPlay({list})
      },
      selectSong(song) {
        this.insertSong(new Song(song))
      },
      back() {
        this.$router.back()
      },
      switchOne(ind) {
        this.switchIndex = ind
      },
      handlePlaylist(playList) {
        let bottom = playList.length > 0 ? '60px' : ''
        this.$refs.listWrapper.style.bottom = bottom
        if (this.$refs.refFavorite && this.$refs.refFavorite.refresh) {
          this.$refs.refFavorite.refresh()
        }
        if (this.$refs.refHistory && this.$refs.refHistory.refresh) {
          this.$refs.refHistory.refresh()
        }
      },
      ...mapActions({
        insertSong: 'insertSong',
        randomPlay: 'randomPlay'
      })
    },
    computed: {
      noResultTitle() {
        if (this.switchIndex === 0) {
          return '您还未添加喜欢的歌曲'
        } else {
          return '您还没有播放记录'
        }
      },
      noResult() {
        if (this.switchIndex === 0) {
          return !this.favoriteList.length
        } else {
          return !this.playHistory.length
        }
      },
      ...mapGetters([
        'favoriteList',
        'playHistory'
      ])
    }
  }

</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
  @import "~common/stylus/variable"

  .user-center
    position: fixed
    top: 0
    bottom: 0
    z-index: 100
    width: 100%
    background: $color-background
    .back
      position: absolute
      top: 0
      left: 6px
      z-index: 50
      .icon-back
        display: block
        padding: 10px
        font-size: $font-size-large-x
        color: $color-theme
    .switches-wrapper
      margin: 10px 0 30px 0
    .play-btn
      margin: 0 auto
      width: 135px
      padding: 7px 0
      box-sizing: border-box
      border: 1px solid  $color-text-l
      border-radius: 100px
      color: $color-text-l
      text-align: center
      font-size: 0
      .icon-play
        display: inline-block
        vertical-align: middle
        margin-right: 6px
        font-size: $font-size-medium-x
      .text
        display: inline-block
        vertical-align: middle
        font-size: $font-size-small
    .list-wrapper
      position: absolute
      top: 110px
      bottom: 0
      width: 100%
      .list-scroll
        height: 100%
        overflow: hidden
        .list-inner
          padding: 20px 30px
    .no-result-wrapper
      position: absolute
      width: 100%
      top: 50%
      transform: translateY(-50%)

</style>
